#!/bin/bash
# 这个脚本从solr的inlink file 中得到现有的inlink和relatesQuestion这两个文件
#步骤如下：
#1、根据输入doc_id.map 挑选出Relates
#2、根据doc_id.map将1中所得到的文件映射成为连续的id
#3、统计新id组成的related file
#
if [ $# -ne 3 ];then
    echo "usage: getInlinkFromSolrRelated.sh solrRelatedFile doc_id.map dstFile";
    exit 1;
else
    solrRelatedFile=$1
    docIdMap=$2
    dstFile=$3
fi

#step 1: select relates
awk '
FILENAME==ARGV[1]{
    #store the doc map 
    docMap[$1] = $2;
}
FILENAME==ARGV[2]{
    if (docMap[$1]) {
        print $0;
    }
}' $docIdMap $solrRelatedFile  > $solrRelatedFile".selected"

#step2: map id
awk '
FILENAME==ARGV[1] {
    #store the doc map 
    docMap[$1] = $2;
}
FILENAME==ARGV[2] {
    #select and map the ratings
    documentId = docMap[$1];
    idLength=split($2, tmpArray, ",");
    printf("%d ", documentId);
    for (dId=1; dId<=idLength;  ++dId) {
        if (length(tmpArray[dId]) < 1) continue;
        original_dId = docMap[tmpArray[dId]];
        if (idLength == dId) {
            printf("%d", original_dId);
        }
        else {
            printf("%d,", original_dId);
        }
    }
    printf("\n");
}'  $docIdMap $solrRelatedFile".selected" > $dstFile

#step3: stat the in link
